package J9_29;

import java.util.Arrays;

public class test {
    public static void main(String[] args) {
        int[] numbers = {5, 3, 8, 4, 2, 7, 1, 6};

        for (int pass = 0; pass < numbers.length - 1; pass++) {
            boolean swapped = false;
            for (int i = 0; i < numbers.length - 1 - pass; i++) {
                if (numbers[i] > numbers[i + 1]) {
                    int temp = numbers[i];
                    numbers[i] = numbers[i + 1];
                    numbers[i + 1] = temp;
                    swapped = true;
                }
            }
            if (!swapped) {
                break; // already sorted, exit early
            }
        }

        System.out.println(Arrays.toString(numbers));
    }
}
